#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class people{
public:
	string name;
	int year;
	int month;
	int day;
	int number;
};

bool operator<(people a,people b){
	if(a.year!=b.year)return a.year<b.year;
	if(a.month!=b.month)return a.month<b.month;
	if(a.day!=b.day)return a.day<b.day;
	return a.number>b.number;
}
int main(void){
	int n=0;
	people temp;
	vector<people> data;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>temp.name>>temp.year>>temp.month>>temp.day;
		temp.number=i;
		data.push_back(temp);
	}
	sort(data.begin(),data.end());
	for(int i=0;i<n;i++){
		cout<<data[i].name<<endl;
	}
	return 0;
}
